package com.yyq;

import com.yyq.Listner.RunRobotHotKeyListener;
import com.yyq.controller.FirController;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * @author yyq
 * @create 2021-10-06 21:22
 */
public class App extends Application {



   @Override
   public void start(Stage primaryStage) throws Exception {
       //保证窗口关闭后，Stage对象仍然存活
       Platform.setImplicitExit(false);
       Scene secene = FirController.getSecene();
       primaryStage.setScene(secene);
       primaryStage.getIcons().add(new Image(App.class.getClassLoader().getResourceAsStream("test.png")));
       primaryStage.hide();
       primaryStage.setResizable(false);
       primaryStage.setTitle("截图上传工具");

       //构建系统托盘图标
       BufferedImage image = ImageIO.read(Objects.requireNonNull(App.class.getClassLoader().getResourceAsStream("test.png")));
       TrayIcon trayIcon = new TrayIcon(image, "截图上传工具");
       trayIcon.setImageAutoSize(true);
       //获取系统托盘
       SystemTray tray = SystemTray.getSystemTray();
       //添加鼠标点击事件
       trayIcon.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               //双击左键
               if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                   Platform.runLater(() -> {
                       if (primaryStage.isIconified()) {
                           primaryStage.setIconified(false);
                       }
                       if (!primaryStage.isShowing()) {
                           primaryStage.show();
                       }
                       primaryStage.toFront();
                   });
               }
               //鼠标右键,关闭应用
               else if (e.getButton() == MouseEvent.BUTTON3) {
                   Platform.setImplicitExit(true);
                   tray.remove(trayIcon);
                   Platform.runLater(primaryStage::close);
                   System.exit(0);
               }
           }
       });
       //添加托盘图标
       tray.add(trayIcon);


       JIntellitype.getInstance().registerHotKey(0, JIntellitype.MOD_SHIFT, (int)'Z');//给变量名为0的值设置热键为R
       JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_SHIFT, (int)'z');//给变量名为0的值设置热键为R
       JIntellitype.getInstance().addHotKeyListener(new RunRobotHotKeyListener());




   }
    public static void main(String[] args) {
       launch(args);
    }
}
